//
//  ByOtherCityPickerView.m
//  oc_knowledge
//
//  Created by mc on 17/1/12.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByOtherCityPickerView.h"

@implementation ByOtherCityPickerView

#pragma mark - getter
-(NSMutableArray *)provinceListArray{
    
    if (_provinceListArray == nil) {
        _provinceListArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _provinceListArray;
}

-(NSMutableArray *)cityListArray{
    
    if (_cityListArray == nil) {
        _cityListArray = [[NSMutableArray alloc] initWithCapacity:0];
        
    }
    return _cityListArray;
}

-(NSMutableArray *)areaListArray{
    
    if (_areaListArray == nil) {
        _areaListArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _areaListArray;
}




#pragma mark - init
-(instancetype)initWithPicker{
    
    if (self = [super init]) {
        [self _initCityData]; // 从数据库中读取获得城市数据
        [self _initViews];
        _isOpen = NO;
    }
    return self;
}
#pragma mark - 创建数据
-(void)_initCityData{
    
    // 获得全部省的数组
    [self.provinceListArray addObjectsFromArray:[ByCoreAddressSqlite3 getAllProvinceModel]];
    ByCoreProvinceModel *province = [self.provinceListArray firstObject];
    
    // 获得全部城市的数组
    [self.cityListArray addObjectsFromArray:[ByCoreAddressSqlite3 getAllCityModelWithProvinceID:province.provinceID]];
    ByCoreCityModel *city = [self.cityListArray firstObject];
    
    // 获得全部区的数组
    [self.areaListArray addObjectsFromArray:[ByCoreAddressSqlite3 getAllAreaModelWithCityID:city.cityID]];
    
    // 总数据
    self.dataArray =[[NSMutableArray alloc] init];
    [self.dataArray addObject:self.provinceListArray];
    [self.dataArray addObject:self.cityListArray];
    [self.dataArray addObject:self.areaListArray];
}
#pragma mark - 创建视图
-(void)_initViews{
    
    [self createCompleteButton];
    [self creatPickerView];
}

// 创建完成按钮
- (void)createCompleteButton {
    
    // 创建完成按钮
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat width = 40;
    CGFloat height = 30;
    x = SCREEN_WIDTH - height - 20;
    UIButton *CompleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    CompleteButton.frame = CGRectMake(x, y, width, height);
    [CompleteButton setTitle:@"完成" forState:UIControlStateNormal];
    [CompleteButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [CompleteButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [CompleteButton addTarget:self action:@selector(completeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:CompleteButton];
    
    // 创建pickerView顶部的线，或者说是与按钮的分界线
    x = 0;
    y = CompleteButton.bottom;
    width = SCREEN_WIDTH;
    height = 0.5;
    UIView *sepView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    sepView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
    [self addSubview:sepView];
    
}


// 创建 pickerView
-(void)creatPickerView{
    
    if (self.pickerView == nil) {
        
        self.backgroundColor = [UIColor clearColor];
        self.bounds = CGRectMake(0, 0, SCREEN_WIDTH, 216+30+0.5);
        self.center = CGPointMake(SCREEN_WIDTH/2.0, SCREEN_HEIGHT + self.height/2.0);
        
        self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 30+0.5, self.width, 216)];
        self.pickerView.delegate = self;
        self.pickerView.dataSource = self;
        self.pickerView.backgroundColor = [UIColor whiteColor];
        [self addSubview: self.pickerView];
    }
}

#pragma mark --- pickView delegate,datasource
// 列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

// 行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [(NSArray *)[self.dataArray objectAtIndex:component] count];
}

// 每行的标题
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    if (component == 0) {
        ByCoreProvinceModel *province = self.provinceListArray[row];
        return province.province;
        
    } else if (component == 1) {
        ByCoreCityModel *city = self.cityListArray[row];
        return city.city;
        
    } else {
        ByCoreAreaModel *area = self.areaListArray[row];
        return area.area;
    }
}


// 选中单元格
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if (component == 0) { // 选中第一列（省份）那么第二列和第三列都变为第一行
        
        self.provinceRow = row;
        
        [self getCityData:row]; // 根据选择的省份重新获取城市数组
        [self getAreaData:0]; // 城市数组为当前城市数组中第一个城市的地区数组
        
        self.provinceRow = row;
        self.cityRow = 0;
        self.areaRow = 0;
        
    } else if (component == 1) { // 选中第二列（城市）那么第一列不变，第三列都变为第一行
        
        self.cityRow = row;
        
        [self getAreaData:row]; // 根据选择的城市重新获取地区数组
        
        self.areaRow = 0;
        
        
    } else { // 选中第三列（地区）那么第一列和第二列都变
        
        self.areaRow = row;
    }
    
    [self.pickerView reloadAllComponents];
    
    [self.pickerView selectRow:self.provinceRow inComponent:0 animated:YES];
    [self.pickerView selectRow:self.cityRow inComponent:1 animated:YES];
    [self.pickerView selectRow:self.areaRow inComponent:2 animated:YES];
    
    
    // 每次点击都获取当前的地址
    ByOtherCityObject *cityObject = [self buildCityObject];
    // 把当前地址回传到控制器里，设置UI
    if ([self.delegate respondsToSelector:@selector(cityPickerViewDidChooseAddress:)]) {
        [self.delegate cityPickerViewDidChooseAddress:cityObject];
    }
    
}

// 创建模型
- (ByOtherCityObject *)buildCityObject{
    
    ByOtherCityObject *object = [[ByOtherCityObject alloc] init];
    // 省
    ByCoreProvinceModel *province = self.provinceListArray[self.provinceRow];
    object.province = province.province;
    object.provinceid =  province.provinceID;
    
    // 市
    if (self.cityListArray.count == 0) { // 某省份下没有市了
        object.city = @"";
        object.cityid  =  @"0";
    } else {
        ByCoreCityModel *city = self.cityListArray[self.cityRow];
        object.city = city.city;
        object.cityid  =  city.cityID;
    }
    
    // 地区
    if (self.areaListArray.count == 0) { // 某市中没有地区了
        object.area = @"";
        object.areaid = @"0";
    } else {
        ByCoreAreaModel *area = self.areaListArray[self.areaRow];
        object.areaid = area.areaID;
        object.area = area.area;
    }
    
    object.fullAddress =[NSString stringWithFormat:@"%@%@%@", object.province, object.city, object.area];
    
    return object;
}

#pragma mark  -- 根据provinceID 读取城市列表
- (void)getCityData:(NSInteger )row{
    
    // 当前省份
    ByCoreProvinceModel *province = self.provinceListArray[row];
    [self.cityListArray removeAllObjects];
    [self.cityListArray setArray:[ByCoreAddressSqlite3 getAllCityModelWithProvinceID:province.provinceID]];
    
    
}
#pragma mark  -- 根据cityID 读取地区列表
-(void)getAreaData:(NSInteger )row{
    
    // 如果没有城市，那么地区也对应没有
    if (self.cityListArray.count == 0) {
        [self.areaListArray removeAllObjects];
        return;
    }
    // 当前城市
    ByCoreCityModel *city = self.cityListArray[row];
    [self.areaListArray removeAllObjects];
    [self.areaListArray setArray:[ByCoreAddressSqlite3 getAllAreaModelWithCityID:city.cityID]];
}

#pragma mark --  open  --
-(void)open {
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    self.center = CGPointMake(SCREEN_WIDTH/2.0, SCREEN_HEIGHT - 64 - self.height/2.0);
    [UIView commitAnimations];
    
    self.isOpen = YES;
    [self sendSelfFrameNotification];
}
#pragma mark --  close  --
-(void)close{
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    self.center = CGPointMake(SCREEN_WIDTH/2.0, SCREEN_HEIGHT - 64 + self.height/2.0);
    [UIView commitAnimations];
    
    self.isOpen = NO;
    [self sendSelfFrameNotification];
}

#pragma mark -- 点击事件 --
- (void)completeAction:(UIButton *)button {
    
    [self close];
}


#pragma mark -- 弹出弹回通知 --
-(void)sendSelfFrameNotification{
    
    CGFloat top = self.top;
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:PickerFrameChange object:self userInfo:@{ZKPickerTop : @(top)}];
}


@end
